home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / CUJ9201.ARJ / 1001126A < prev    next >
Text File  |  1992-06-02  |  2KB  |  59 lines

  1. #include <iostream.h>
  2. #include <iomanip.h>
  3.  
  4. // Crude date class to demonstrate
  5. // customized stream I/O -- no error checking attempted
  6.  
  7. class date
  8.       {
  9.       unsigned int year;  // 0-99
  10.       unsigned int mon;   // 1-12
  11.       unsigned int day;   // 1-31
  12. // Allow the I/O stream operators to access the private members
  13. // of the date class. You can't define these as members
  14. // because the first argument is a stream, not a date.
  15.       friend ostream & operator <<(ostream &s,date dt);
  16.       friend istream & operator >>(istream &s,date &dt);
  17. public:
  18. // constructor
  19.       date(int _mon=1,int _day=1,int _year=0)
  20.           { mon=_mon; day=_day; year=_year; }
  21.       };
  22.  
  23. // Output a date as: MM/DD/YY
  24. // No attempt was made to pad the elements with zeros
  25. ostream & operator <<(ostream &s,date dt)
  26.    {
  27.    s << dt.mon << "/" << dt.day << "/" << dt.year;
  28.    return s;
  29.    }
  30.  
  31. // Input a date in the format: MM/DD/YY -- allow any character
  32. // to seperate the elements (i.e., MM-DD-YY, MM,DD,YY, etc.)
  33. // Notice that the date is a reference (&dt) so we modify
  34. // the actual date -- not a copy passed by value.
  35. istream & operator >>(istream &s,date &dt)
  36.    {
  37.    int m,d,y;
  38.    char dummy;  // this char holds the seperator
  39.    s >> m >> dummy >> d >> dummy >> y;
  40.    dt.mon=m;
  41.    dt.day=d;
  42.    dt.year=y;
  43.    return s;
  44.    }
  45.  
  46. // Simple demo for dates. Notice how the stream I/O operators
  47. // have been overloaded to accept the date class.
  48. main()
  49.   {
  50.   date jan1(1,1,70);
  51.   date bday;
  52.   cout << "Enter your birthday (MM/DD/YY): ";
  53.   cin >> bday;
  54.   cout << "The first date is " << jan1 << "\n";
  55.   cout << "Your birthday is " << bday << "\n";
  56.   cout << 1;
  57.   }
  58. // End of File
  59.